package com.qst.controller;

import com.qst.Service.LoginuserService;
import com.qst.pojo.Admin;
import com.qst.pojo.User;
import com.qst.utils.RandomValidateCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @packageName:com.qst.controller
 * @className:LoginController
 * @Description:
 * @author:zhangyu
 * @date 2021/6/17 15:23
 **/

@Controller
public class LoginController {
    //controller层调用Service层

    @RequestMapping(value = "/login")
    public void login(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("image/jpeg");

        response.setHeader("pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");

        response.setDateHeader("Expire", 0);

        RandomValidateCode randomValidateCode = new RandomValidateCode();
        try {
            randomValidateCode.getRandcode(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Resource
    private LoginuserService loginuserService;

    //建立一个session，用来将模一个用户数据导入其中，用于后期其他部分的调用
    @RequestMapping(value = "/tologin",method= RequestMethod.POST)
    public String login(User loginUser, HttpServletRequest request, HttpSession session, Admin admin) {
        boolean flag = loginuserService.finduser(loginUser.getLoginname(), loginUser.getPassword());
        String loginname = request.getParameter("loginname");
        String password = request.getParameter("password");
        boolean mag = loginuserService.getaminbyname(loginname,password);
        System.out.println(mag);
        session.setAttribute("selectfromloginname", loginname);
        String code = request.getParameter("inputStr");
        String random = (String) session.getAttribute("randomcode_key");
        if (flag && random.equals(code)) {
            return "/main";
        } else {
            if (mag && random.equals(code)) {
                    return "/admin/adminmain";
                } else {
                    return "/login";
                }
            }


        }


    @RequestMapping(value = "/user")
    public ModelAndView order1(String loginname,HttpSession session) {
        String selectloginname = (String)session.getAttribute("selectfromloginname");
        List<User> users = loginuserService.getuserbyname(selectloginname);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userAll", users);
        modelAndView.setViewName("newdao");
        return modelAndView;
    }
//    @RequestMapping(value = "/out")
//    public ModelAndView out(HttpSession session){
//        session.invalidate();
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("login");
//        return modelAndView;
//    }




}
